#########################################################

config_path=/public/home/xxf2019/20220915_gastric_multiple/rna_batch1/config
raw_fastq_path=/public/home/xxf2019/20220915_gastric_multiple/rna_batch1/raw_fastq
fastq_path=/public/home/xxf2019/20220915_gastric_multiple/rna_batch1/fastq

mkdir -p ${config_path}
mkdir -p ${fastq_path}

#########################################################
## 产生patient_list
echo "sample,seq_type" > ${config_path}/patients.csv

find ${raw_fastq_path} | grep fq.gz | awk -F'/' '{print $(NF-1)}' | sort -u | \
awk '{OFS=","}{print $1,"mRNA"}' >> ${config_path}/patients.csv

#########################################################
## 链接fastq文件
for sample in `cat ${config_path}/patients.csv | sed '1d' | awk -F',' '{print $1}'`
do
echo ${sample}
fq_1=`find ${raw_fastq_path} | grep 1.fq.gz | grep ${sample}`
fq_2=`find ${raw_fastq_path} | grep 2.fq.gz | grep ${sample}`

cat ${fq_1} > ${fastq_path}/${sample}.R1.clean.fastq.gz
cat ${fq_2} > ${fastq_path}/${sample}.R2.clean.fastq.gz

md5sum ${fastq_path}/${sample}.R1.clean.fastq.gz > ${fastq_path}/${sample}.R1.clean.fastq.gz.md5
md5sum ${fastq_path}/${sample}.R2.clean.fastq.gz > ${fastq_path}/${sample}.R2.clean.fastq.gz.md5
done
cat ${fastq_path}/*.clean.fastq.gz.md5 > ${fastq_path}/All.Fastq.md5

rm ${fastq_path}/Raw.Fastq.md5
## 原始的md5
for sample in `cat ${config_path}/patients.csv | sed '1d' | awk -F',' '{print $1}'`
do
echo ${sample}
md5_file=`find ${raw_fastq_path}/${sample}/MD5.txt`
cat ${md5_file} >>  ${fastq_path}/Raw.Fastq.md5
done

#########################################################

cd /public/home/xxf2019/20220913_NextFlow_RNA
work_dir=/public/home/xxf2019/20220915_gastric_multiple/rna_batch1
inputdir=/public/home/xxf2019/20220915_gastric_multiple/rna_batch1/fastq

#########################################################
## fastqc质控

start=fastqc

sh RNA-Seq.sh --outdir ${work_dir}/results --design ${work_dir}/config/patients.csv \
-profile babs --inputdir ${inputdir} --start ${start} -resume


#########################################################
## RNA比对和定量

start=start

sh RNA-Seq.sh --outdir ${work_dir}/results --design ${work_dir}/config/patients.csv \
-profile babs --inputdir ${inputdir} --start ${start} -resume

#########################################################
## bam质控

start=bamqc
inputdir=${work_dir}/results/STAR

sh RNA-Seq.sh --outdir ${work_dir}/results --design ${work_dir}/config/patients.csv \
-profile babs --inputdir ${inputdir} --start ${start} -resume